/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wap.rockvilletech.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Usman
 */
public class Paging {
    
    private static int intBlock = 0,
                       intNextBlock = 0,
                       intPrevousPage = 0,
                       intNextPage = 0,
                       intPrvBlock = 0;
    
    private static String strPagingLinks = "",
                          strPagingSummary = "";
    
    public static String getPagingSummary() {
        return strPagingSummary;
    }
    public static String getPagingLinks() {
        return strPagingLinks;
    }
    
    /**
     * This is a custom Paging for 
     * @param request
     * @param myURL
     * @param intRecordsToBeFetched
     * @param intRecordsToBeDisplayed
     * @param intRecCount
     * @param intBlockLength
     * @param intLastPage
     * @param intPageID
     * @param isNext
     * @Note 
     * 1 - [First/Previous Block/Previous Page] 1 2 3 [Next Page/Next Block/Last]
     * 2 - get the params from request object (which are not related to paging) and add them to each page url which will retain the filtering params
     */
    public static void computePaging(HttpServletRequest request, String myURL, int intRecordsToBeFetched, 
                              int intRecordsToBeDisplayed, int intRecCount, int intBlockLength, 
                              int intLastPage, int intPageID, boolean isNext) {
        
        String strTemp = "",
               filterParams = "";
        
        // Logic to retain filter variables - START
        Map mp = new HashMap();
        mp = request.getParameterMap();        
        Set entries = mp.entrySet();
        Iterator itr = entries.iterator();
        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry)itr.next();
            String key = entry.getKey().toString();  // Get the key from the entry.
            // Skip the keys that r not necessary
            if (key.equalsIgnoreCase("check") || key.equalsIgnoreCase("url") || key.equalsIgnoreCase("bid") || key.equalsIgnoreCase("pid") || key.equalsIgnoreCase("picid")) { }
            else {
                String value = ((String[])entry.getValue())[0];
                if (value.equalsIgnoreCase("") || value.length() < 1) { }
                else {
                    filterParams += "&"+ key +"="+ value;
                }
                //log.debug( "   (" + key + "," + value + ")" );
            }
        }
        // Logic to retain filter variables - END
        
        int tempRec = intRecordsToBeFetched + intRecordsToBeDisplayed;
        if (tempRec > intRecCount)
            tempRec = intRecCount;
        
        strPagingSummary = "<span id='summary'>";
        
        if(intRecCount == 1) {
            strPagingSummary += "Showing the only record found";
        }
        else if(intRecCount <= intRecordsToBeDisplayed) {
            strPagingSummary += "Showing All "+ tempRec +" records found";
        }
        else {
            strPagingSummary += "Currently showing ("+(intRecordsToBeFetched)+" to "+tempRec+") of "+intRecCount+" records";
        }
        
        strPagingSummary += "</span>";
        
        try {
            //intBlock = Integer.parseInt( request.getParameter("bid").trim() ); // Block ID
            intBlock = (int)(intPageID / intBlockLength);
        } catch (Exception ex) { intBlock = 0; }

        if (request.getParameter("check") != null && request.getParameter("check").equalsIgnoreCase("prv")) {
            if(intBlock > 0) {
                intPrvBlock = intBlock - 1;
                intPrevousPage = intBlock * intBlockLength - intBlockLength;
                intNextBlock = intBlock;
                intNextPage = intBlock * intBlockLength + intBlockLength;
            }
            else {
                intNextBlock = intBlock + 1;
                intNextPage = intBlock * intBlockLength + intBlockLength;
            }
        }
        else if (request.getParameter("check") != null && request.getParameter("check").equalsIgnoreCase("nxt")) {
            if (intBlock > 0) {
                intPrvBlock = intBlock - 1;
            }
            else {
                intPrvBlock = intBlock;
            }
            intNextBlock = intBlock + 1;
            intNextPage = intNextBlock * intBlockLength;
            intPrevousPage = intBlock * intBlockLength - intBlockLength;
        }
        else {
            intNextBlock = intBlock + 1 ;
            if(intBlock > 0) {
                intPrvBlock = intBlock - 1;
                intPrevousPage = intBlock * intBlockLength - intBlockLength;
            }
            else {
                intPrvBlock = intBlock;
            }
            intNextPage = intBlock * intBlockLength + intBlockLength;
        }

        if (intNextPage > intLastPage) isNext = false;
        
        strPagingLinks = "<span id='paging'>";
        
        // compute weather to show 'previous Blog' link
        if (intBlock != 0) {
            //strPagingLinks += "<a href='"+myURL+"&pid="+intPrevousPage+"&bid="+intPrvBlock+"&check=prv"+filterParams+"'>&lt;&lt;</a>";
            strPagingLinks += "<a class='LinkFoot' href='"+myURL+"&pid="+intPrevousPage+"&check=prv"+filterParams+"'>&lt;&lt;</a>";
        }
        
        // compute weather to show 'previous Page' link
        if ((intPageID - 1) >= 0) {
            if (intPageID > (intBlock * intBlockLength)) {
                //strTemp = (intPageID - 1) +"&bid="+ intBlock;
                strTemp = (intPageID - 1) +"";
            }
            else {
                //strTemp = (intPageID - 1) +"&bid="+ (intBlock - 1) +"&check=prv";
                strTemp = (intPageID - 1) +"&check=prv";
            }
            strPagingLinks += "<a class='LinkFoot' href='"+myURL+"&pid="+ strTemp + filterParams +"'>&nbsp;&nbsp;&lt;</a>";
        }
        
        // Compute page links
        for (int x = (intBlock * intBlockLength + 1); x <= (intBlock + 1) * intBlockLength ;x++) {
            if ((x-1 <= intLastPage) && ((x-1)*intRecordsToBeDisplayed < intRecCount)) {
                if (x-1 == intPageID) {
                    strPagingLinks += "&nbsp;&nbsp;<span class='pagingLinks'>"+ x +"</span>";
                }
                else {
                    //strPagingLinks += "<a href='"+myURL+"&pid="+ (x-1) +"&bid="+intBlock+filterParams+"'>&nbsp;&nbsp;"+ x +"</a>";
                    strPagingLinks += "<a class='LinkFoot' href='"+myURL+"&pid="+ (x-1) + filterParams+"'>&nbsp;"+ x +"</a>";
                }
            }
        }
        
        // compute weather to show 'next Page' link
        if (intPageID < intLastPage) {
            if ((intPageID + 1) < ((intBlock + 1) * intBlockLength)) {
                //strTemp = (intPageID + 1) +"&bid="+ intBlock;
                strTemp = (intPageID + 1) +"";
            }
            else {
                //strTemp = (intPageID + 1) +"&bid="+ (intBlock + 1) +"&check=nxt";
                strTemp = (intPageID + 1) +"&check=nxt";
            }
            //LinkIndex
            strPagingLinks += "<a class='LinkFoot' href='"+myURL+"&pid="+ strTemp + filterParams+"'>&nbsp;&gt;</a>";
        }
        
        // compute weather to show 'next Blog' link
        if (isNext) {
            if ((intPageID + 1) < ((intBlock + 1) * intBlockLength)) {
                strTemp = Integer.toString(intNextBlock);
            }
            else {
                strTemp = Integer.toString(intNextBlock + 1);
            }
            //strPagingLinks += "<a href='"+myURL+"&pid="+intNextPage+"&bid="+strTemp+"&check=nxt"+filterParams+"'>&nbsp;&nbsp;&gt;&gt;</a>";
            strPagingLinks += "<a class='LinkFoot' href='"+myURL+"&pid="+intNextPage+"&check=nxt"+filterParams+"'>&nbsp;&nbsp;&gt;&gt;</a>";
        }
        strPagingLinks += "</span>";
    }
}
